package com.example.device.vm

import com.example.common.view.vm.BaseViewModel
import com.example.device.api.DeviceApiService
import com.example.device.entity.DeviceEntityItem
import com.example.device.entity.DeviceParam
import com.example.device.model.DeviceRepository
import com.example.device.model.DeviceResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DeviceViewModel @Inject constructor(private val apiService: DeviceApiService):BaseViewModel<DeviceRepository,DeviceResultData<List<DeviceEntityItem>>>(){
    fun getListData(){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.getDevice()
                val res=DeviceResultData(
                    state=result.state,
                    msg=result.msg,
                    data=result.data
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getAddListData(deviceParam: DeviceParam){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.addDevice(deviceParam)
                val res=DeviceResultData(
                    state=result.state,
                    msg=result.msg,
                    data= listOf(result.data)
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
}